﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class BattleSceneManager : MonoBehaviour
{
    private void OnEnable()
    {
        EventCenter.GetInstance().AddEventListener("Win", WinSceneChange);
        EventCenter.GetInstance().AddEventListener("Defeat", LoseSceneChange);
    }
    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener("Win", WinSceneChange);
        EventCenter.GetInstance().RemoveEventListener("Defeat", LoseSceneChange);
    }

    private void WinSceneChange()
    {
        StartCoroutine(LoadWinSceneAfterAnimation());
    }
    public void LoseSceneChange()
    {
        StartCoroutine (LoadLoseSceneAfterAnimation());
    }

    private IEnumerator LoadLoseSceneAfterAnimation()
    {

        Scene currentActiveScene = SceneManager.GetActiveScene();
        AsyncOperation ao = SceneManager.LoadSceneAsync("SelectCard", LoadSceneMode.Additive); // 使用 Additive 模式加载新场景
        Scene newScene = SceneManager.GetSceneByName("SelectCard");
        yield return ao;
        // 设置新加载的场景为活跃场景
        SceneManager.SetActiveScene(newScene);
        // 卸载原来的活跃场景
        SceneManager.UnloadSceneAsync(currentActiveScene);
    }
    private IEnumerator LoadWinSceneAfterAnimation()
    {
        Scene currentActiveScene = SceneManager.GetActiveScene();
        AsyncOperation ao = SceneManager.LoadSceneAsync("ScendDialogScene", LoadSceneMode.Additive); // 使用 Additive 模式加载新场景
        Scene newScene = SceneManager.GetSceneByName("ScendDialogScene");
        yield return ao;
        // 设置新加载的场景为活跃场景
        SceneManager.SetActiveScene(newScene);
        // 卸载原来的活跃场景
        SceneManager.UnloadSceneAsync(currentActiveScene);
    }
}
